//
//  MapTableViewCell.m
//  Findif
//
//  Created by Mac on 2018/11/21.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MapTableViewCell.h"
//地图
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
//#import <AMapLocationKit/AMapLocationKit.h>
//#import <MapKit/MapKit.h>

@interface MapTableViewCell ()<MAMapViewDelegate>

@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, strong)MAMapView * mapView;

@end


@implementation MapTableViewCell


- (void)setLocal:(NSString *)local{
    
    _local = local;
    
    NSArray *array = [local componentsSeparatedByString:@","];
    
    double latitude = [array.firstObject doubleValue];
    double longitude = [array.lastObject doubleValue];
    
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
    [self addAnnotationwith:0 WithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ZRWhiteColor;
    
    ///初始化地图
    self.mapView = [[MAMapView alloc] initWithFrame:self.bounds];
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    self.mapView.showsCompass = NO;
    self.mapView.showsScale = NO;
    self.mapView.scrollEnabled = NO;
    self.mapView.zoomEnabled = NO;
    self.mapView.rotateEnabled = NO;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.mapView addGestureRecognizer:tapGR];
    
    [self.mapView setZoomLevel:14 animated:YES];
//    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
//    [self.mapView setCenterCoordinate:(CLLocationCoordinate2D)];
    
    ///把地图添加至view
    [self.contentView addSubview:self.mapView];
}

/* 实现代理方法：*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAUserLocation class]]){
        static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:userLocationStyleReuseIndetifier];
        }
        
        annotationView.image = [UIImage imageNamed:@"我的位置"];
        
        return annotationView;
        //([annotation isKindOfClass:[MAPointAnnotation class]])
    }else{
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation                                                 reuseIdentifier:pointReuseIndetifier];
        }
        annotationView.canShowCallout = NO;
        annotationView.animatesDrop = YES;
        annotationView.draggable = YES;
        annotationView.image = [UIImage imageNamed:@"导航坐标"];
        annotationView.rightCalloutAccessoryView  = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        
        
        
        return annotationView;
    }
}


- (void)addAnnotationwith:(NSInteger)index WithCoordinate:(CLLocationCoordinate2D)coordinate {
    
    MAPointAnnotation * a = [[MAPointAnnotation alloc] init];
    
    a.coordinate = coordinate;
    
    [self.mapView addAnnotation:a];
    
}


- (void)tapAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PushToNavViewController" object:nil];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
